<!--
 * @Author: your name
 * @Date: 2021-07-12 15:21:07
 * @LastEditTime: 2021-07-21 11:25:12
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /shb-pc-fe/src/modules/setting/productV2/productSet/components/addDialog.vue
-->
<template>
  <base-modal :title="title" :show.sync="visible" width="800px">
    <el-form label-position="top" :model="form" class="product-menu-form">
      <el-form-item label="">
        <el-input v-model.trim="form.catalogName" :placeholder="$t('product.setting.formDialog.pla')" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{$t('common.base.cancel')}}</el-button>
      <el-button type="primary" @click="save(type)">{{$t('common.base.confirm')}}</el-button>
    </div>
  </base-modal>
</template>p
<script>
import {
  saveProductMenu,
  updateTree
} from '@src/api/ProductV2Api';
import i18n from '@src/locales'
export default {
  data() {
    return {
      visible: false,
      title: i18n.t('product.setting.formDialog.title'),
      form:{
          catalogName:''
      },
      type:1,   //1是新建。0是编辑
      id:''
    }
  },
  methods: {
    async save(){
      let res;
      if(this.type){
        res=await saveProductMenu(this.form)
      }else{
        res=await updateTree({
          id:this.id,
          catalogName:this.form.catalogName
        })
      }
      if(res.code===0){
          this.$message({
            showClose: true,
            duration: 1500,
            message: this.type? this.$t('common.base.tip.createSuccess2') : this.$t('common.base.tip.edit2Success'),
            type: "success",
          });
          this.visible=false
          this.$emit('refresh')
      }else{
           this.$message({
            showClose: true,
            duration: 1500,
            message: res.message,
            type: "error",
          });
      }  
    },
    openDialog(title,row){
      this.visible=true
      if(title==='create'){
        this.form.catalogName=''
        this.title = this.$t('product.setting.formDialog.title2')
        this.type=1
      }else{
        this.type=0
        this.form.catalogName=row.catalogName
        this.id=row.id
        this.title = this.$t('product.setting.formDialog.title3')
      }
    }
  },
}
</script>
<style scoped lang="scss">
.product-menu-form{
  margin:10px 20px;
}
</style>